package adv6;

import java.io.*;
import java.util.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class SuperMarket implements Serializable {
    private String name;
    private String addr;
    private List<Vip> customers = new ArrayList<>();

    public static final File objpath = new File("D:/tmp/chaoshi.txt");

    public SuperMarket() {
    }

    private SuperMarket(String name, String addr) {
        this.name = name;
        this.addr = addr;
    }
    //从序列化的文件中读取（还原）超市对象
    public static SuperMarket get() {
        ObjectInputStream oin = null;
        try {
            oin=new ObjectInputStream(new FileInputStream(objpath));
            return (SuperMarket) oin.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                oin.close();
            } catch (Exception e) {

            }
        }
        return null;
    }
    public static void save(SuperMarket m){
        try {
            ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(objpath));
            out.writeObject(m);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void add(Vip v) {
        this.customers.add(v);
    }

    public void show() {
        System.out.println(name + ",位于" + addr);
        System.out.println("会员的信息如下：");
        for (Vip v : customers) {
            System.out.println(v);
        }
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    //public static SuperMarket get()
}
